﻿using Zhaoxi.Communication.Base;
using Zhaoxi.Configuration.Base;

namespace Zhaoxi.Communication.Protocols
{
    public class S7Comm : CommBase
    {
        S7.Net.Plc plc;
        public override void Connect(params CommParamEntity[] args)
        {
            string ip = "127.0.0.1";
            var p_ip = args.Where(p => p.name == "IP")
                .FirstOrDefault();
            if (p_ip != null) ip = p_ip.value;

            string port = "102";
            var p_port = args.Where(p => p.name == "Port")
                .FirstOrDefault();
            if (p_port != null) port = p_port.value;

            // 机架和插槽   


            plc = new S7.Net.Plc(
                S7.Net.CpuType.S71200,
                ip,
                int.Parse(port),
                0,
                0);
            plc.Open();
        }

        public override void StartRead(params VariableEntity[] variables)
        {
            foreach (var v in variables)
            {
                v.Value = plc.Read(v.address);
            }
        }
    }
}
